const path = require('path')
module.exports = function (grunt) {
  require("load-grunt-tasks")(grunt); // npm install --save-dev load-grunt-tasks
  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    watch: {
      // 根据不同文件的修改执行不同的生成任务

      load: {
        files: ['src/js/load/*.js'],
        tasks: ['concat:load', 'uglify:load'],
        options: {
          spawn: false,
        },
      },
      // 监控电脑js变化，执行对应的生成任务
      dn: {
        files: ['src/js/dn/*.js'],
        tasks: ['concat:dn', 'uglify:dn'],
        options: {
          spawn: false,
        },
      },
      // 监控手机js变化，执行对应的生成任务
      sj: {
        files: ['src/js/sj/*.js'],
        tasks: ['concat:sj', 'uglify:sj'],
        options: {
          spawn: false,
        },
      },
      // 监控公用的js文件变化，执行全部电脑和手机的js更新
      public: {
        files: ['src/js/public/*.js'],
        tasks: ['concat', 'uglify'],
        options: {
          spawn: false,
        },
      },
      // 监控电脑端的scss变化
      /* css需要先scss转换成css 再执行兼容浏览器，最后再压缩 */
      dncss: {
        files: ['src/scss/dn.scss'],
        tasks: ['sass:dn', 'autoprefixer:dn','cssmin:dn'],
        options: {
          spawn: false,
        },
      },
      sjcss: {
        files: ['src/scss/sj.scss'],
        tasks: ['sass:sj', 'autoprefixer:sj','cssmin:sj'],
        options: {
          spawn: false,
        },
      },
      cscss: {
        files: ['src/scss/cs.scss'],
        tasks: ['sass:cs'],
        options: {
          spawn: false,
        },
      },
      //修改公共文件时候，更新全部的css
      publiccss: {
        files: ['src/scss/public/*.scss'],
        tasks: ['sass', 'autoprefixer','cssmin'],
        options: {
          spawn: false,
        },
      },
    },
    // 合并js
    concat: {
      load: { // load图片js
        src: ['src/js/load/*.js'],
        dest: '<%= pkg.outputPath %>/js/load.js'
      },
      dn: { // 电脑端全部的js
        src: ['src/js/public/swiper.min.js','src/js/public/xh-form.js', 'src/js/dn/*.js'],
        dest: '<%= pkg.outputPath %>/js/main.js'
      },
      sj: { // 手机端全部的js
        src: ['src/js/public/swiper-bundle.min.js','src/js/public/xh-form.js', 'src/js/sj/*.js'],
        dest: '<%= pkg.outputPath %>/js/sj.js'
      }
    },
    // scss 生成css ,scss就不合并了，全部采用自行引入方式
    sass: {
      options: { // Target options
        style: 'expanded',
        sourceMap: false
      },
      dn: { // Target
        files: { // Dictionary of files
          '<%= pkg.outputPath %>/css/main.css': ['src/scss/dn.scss']
        }
      },
      sj: { // Target
        files: { // Dictionary of files
          '<%= pkg.outputPath %>/css/sj.css': ['src/scss/sj.scss']
        }
      }
    },
    // 自动添加css前缀
    autoprefixer: {
      options: {
        //任务设置
        browsers: ['> 0.05% in US', 'last 2 versions', 'ie 8', 'ie 9'],
        map: true
      },
      dn: {
        src: 'app/basic/css/main.css', //需要加前缀的css文件               
        dest: 'app/basic/css/main.css' //grunt处理后生成的css文件，如果文件夹中没有该文件，则自动创建
      },
      sj: {
        options: {
          // 手机端降低兼容
          browsers: [ 'last 1 versions','> 5% in US'],
        },
        src: 'app/basic/css/sj.css', //需要加前缀的css文件               
        dest: 'app/basic/css/sj.css' //grunt处理后生成的css文件，如果文件夹中没有该文件，则自动创建
      },
    },
    // css 压缩
    cssmin: {
      options: {
        stripBanners: true, //合并时允许输出头部信息
        banner: '/* \n* <%= pkg.link %>\n* @Author:<%= pkg.author %>  \n* @LastEditTime :<%= grunt.template.today("yyyy-mm-dd") %>\n */\n'
      },
      dn: {
        src: ['<%= pkg.outputPath %>/css/main.css'],
        dest: '<%= pkg.outputPath %>/css/main.min.css'
      },
      sj: {
        src: ['<%= pkg.outputPath %>/css/sj.css'],
        dest: '<%= pkg.outputPath %>/css/sj.min.css'
      }
    }, 
    // js 压缩打包
    uglify: {
      options: {
        mangle: true, // 是否混淆变量名 默认true
        banner: '/* \n* <%= pkg.link %>\n* @Author:<%= pkg.author %>  \n* @LastEditTime :<%= grunt.template.today("yyyy-mm-dd") %>\n */\n'
      },
      load: { // load图片压缩min.js
        files: {
          '<%= pkg.outputPath %>/js/load.min.js': '<%= pkg.outputPath %>/js/load.js'
        }
      },
      dn: { // 电脑js压缩min.js
        files: {
          '<%= pkg.outputPath %>/js/main.min.js': '<%= pkg.outputPath %>/js/main.js'
        }
      },
      sj: { // 手机js压缩min.js
        files: {
          '<%= pkg.outputPath %>/js/sj.min.js': '<%= pkg.outputPath %>/js/sj.js'
        }
      }
    },
    // 创建本地服务，监控文件变化自动更新页面
    browserSync: {
      dev: {
        bsFiles: {
          src: ['app/basic/css/*.css','app/basic/js/*.js', 'app/htm/*/*.html', 'app/htm/**/*.htm']
        },
        options: {
          watchTask: true,
          server: './app'
        }
      }
    }
  })

  grunt.loadNpmTasks('grunt-contrib-uglify');
  // 本地服务部分
  grunt.loadNpmTasks('grunt-browser-sync');
  // 监控部分
  grunt.loadNpmTasks('grunt-contrib-watch');
  // scss css部分
  grunt.loadNpmTasks('grunt-contrib-sass');
  grunt.loadNpmTasks('grunt-autoprefixer');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  // 默认被执行的任务列表。
  grunt.registerTask('default', ['concat', 'sass', 'autoprefixer', 'cssmin', 'uglify','browserSync', 'watch']);

};